package model

import (
	"gorm.io/gorm"
	"log"
)

type ReserveStock struct {
	gorm.Model
	Username       string `gorm:"type:varchar(50); not null; comment:'用户姓名'"`
	IdCard         string `gorm:"type:char(18); not null; comment:'身份证号码'"`
	Phone          string `gorm:"type:char(11); not null; comment:'手机号码'"`
	HospitalId     int64  `gorm:"type:int; not null; comment:'医院Id'"`
	PackageId      int64  `gorm:"type:int; not null; comment:'套餐Id'"`
	ReserveDate    int64  `gorm:"type:int; not null; comment:'具体日期'"`
	TotalStock     int64  `gorm:"type:int; not null; comment:'总库存'"`
	UsdStock       int64  `gorm:"type:int; not null; comment:'已被预约库存'"`
	RemainingStock int64  `gorm:"type:int; not null; comment:'剩余预约库存'"`
	Status         int64  `gorm:"type:int; not null; comment:'0 - 可预约，1 - 已满，2 - 关闭预约'"`
}

func (r *ReserveStock) Create30DayReserveStock(db *gorm.DB) {
	for i := 0; i < 30; i++ {
		err := db.Model(&ReserveStock{}).Where("id=?", r.ID).Find(&i).Error
		if err != nil {
			panic(err)
		}
		log.Print("三十天预约库存已开始")
	}
}
func (r ReserveStock) UpdateRemainingStock(db *gorm.DB, stock int64) error {
	err := db.Model(&ReserveStock{}).Where("id=?", r.ID).Update("remaining_stock", gorm.Expr("remaining_stock-?", stock)).Error
	return err
}
func (r ReserveStock) UpdateRemainingStatus(db *gorm.DB) error {
	err := db.Model(&ReserveStock{}).Where("id=?", r.ID).Update("status", 2).Error
	return err
}
